package com.sf.iguess.survey.service.impl;

import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sf.iguess.survey.domain.Answer;
import com.sf.iguess.survey.domain.UserScore;
import com.sf.iguess.survey.mapper.AnswerDao;
import com.sf.iguess.survey.service.AnswerService;
import com.sf.iguess.survey.service.UserScoreService;

@Service
public class AnswerServiceImpl implements AnswerService {
	
	@Resource
	private AnswerDao answerDao;
	
	@Resource
	private UserScoreService userScoreService;

	@Override
	public boolean save(Answer answer) {
		String answerId = answer.getId();
		if(null == answerId){
			answer.setId(UUID.randomUUID().toString());
			return answerDao.save(answer);
		}else{
			return update(answer);
		}
	}
	
	@Override
	public boolean update(Answer answer){
		return answerDao.update(answer);
	}

	@Override
	public List<Answer> findByScoreId(String scoreId) {
		return answerDao.findByScoreId(scoreId);
	}
	
	@Override
	public void saveAnswer(List<Answer> answerList,UserScore userScore){
		userScoreService.save(userScore);
		
		for (Answer answer : answerList) {
			answer.setScoreId(userScore.getScoreId());
			save(answer);
		}
	}
}
